(use gauche.process)
;;(define path "/track/xsl -f=..\\..\\xsl\\artist.xsl Alive.xml")
;;(define path "/track/cat -n=\\\\item *.xml")
;;(define path "/track/cat -n=\\\\item *.xml | xsl -f=..\\..\\xsl\\artist.xsl")
;;(define path "/track/cat -n=\\\\item *.xml | sort -n=\\\\item -k=\\\\albumYear\\text() -o=-nr")
;;(define path "/track/cat -n=\\\\item *.xml | sort -n=\\\\item -k=\\\\albumYear\\text() -o=-nr | xsl -f=../../xsl/artist.xsl")

(define (adhoc-shell-cmd path)
  (define (backslash->slash str)
    (or (regexp-replace-all #/\\/ str "/") str))

  (rxmatch-cond 
    ((#/^(\S*)\/xsl -f=(\S+) (\S+)$/ path)
     (#f dir xsl-f target)
     (string-append "/home/teruaki/garakuta-okiba/wnix/xsl.scm"
		    " -f=\"" (backslash->slash xsl-f) "\""
		    " ./root" dir "/" (backslash->slash target)))
    ((#/^(\S*)\/cat -n=(\S+) (\S+)$/ path)
     (#f dir n target)
     (string-append "/home/teruaki/garakuta-okiba/wnix/cat.scm"
		    " -n=\"" (backslash->slash n) "\""
		    " ./root" dir "/" (backslash->slash target)))
    ((#/^(\S*)\/cat -n=(\S+) (\S+) \| sort -n=(\S+) -k=(\S+) -o=(\S+)$/ path)
     (#f dir n target sort-n sort-k sort-o)
     (string-append "/home/teruaki/garakuta-okiba/wnix/cat.scm"
		    " -n=\"" (backslash->slash n) "\""
		    " ./root" dir "/" (backslash->slash target)
		    " | "
		    "/home/teruaki/garakuta-okiba/wnix/sort.scm"
		    " -n=\"" (backslash->slash sort-n) "\""
		    " -k=\"" (backslash->slash sort-k) "\""
		    " -o=\"" (backslash->slash sort-o) "\""))
    ((#/^(\S*)\/cat -n=(\S+) (\S+) \| grep -n=(\S+) -k=(\S+) -o=(\S+)$/ path)
     (#f dir n target grep-n grep-k grep-o)
     (string-append "/home/teruaki/garakuta-okiba/wnix/cat.scm"
		    " -n=\"" (backslash->slash n) "\""
		    " ./root" dir "/" (backslash->slash target)
		    " | "
		    "/home/teruaki/garakuta-okiba/wnix/grep.scm"
		    " -n=\"" (backslash->slash grep-n) "\""
		    " -k=\"" (backslash->slash grep-k) "\""
		    " -o=\"" (backslash->slash grep-o) "\""))
    ((#/^(\S*)\/cat -n=(\S+) (\S+) \| sort -n=(\S+) -k=(\S+) -o=(\S+) \| xsl -f=(\S+)$/ path)
     (#f dir n target sort-n sort-k sort-o xsl-f)
     (string-append "/home/teruaki/garakuta-okiba/wnix/cat.scm"
		    " -n=\"" (backslash->slash n) "\""
		    " ./root" dir "/" (backslash->slash target)
		    " | "
		    "/home/teruaki/garakuta-okiba/wnix/sort.scm"
		    " -n=\"" (backslash->slash sort-n) "\""
		    " -k=\"" (backslash->slash sort-k) "\""
		    " -o=\"" (backslash->slash sort-o) "\""
		    " | "
		    "/home/teruaki/garakuta-okiba/wnix/xsl.scm"
		    " -f=\"" (backslash->slash xsl-f) "\""))
    ((#/^(\S*)\/cat -n=(\S+) (\S+) \| sort -n=(\S+) -k=(\S+) -o=(\S+) \| grep -n=(\S+) -k=(\S+) -o=(\S+)$/ path)
     (#f dir n target sort-n sort-k sort-o grep-n grep-k grep-o)
     (string-append "/home/teruaki/garakuta-okiba/wnix/cat.scm"
		    " -n=\"" (backslash->slash n) "\""
		    " ./root" dir "/" (backslash->slash target)
		    " | "
		    "/home/teruaki/garakuta-okiba/wnix/sort.scm"
		    " -n=\"" (backslash->slash sort-n) "\""
		    " -k=\"" (backslash->slash sort-k) "\""
		    " -o=\"" (backslash->slash sort-o) "\""
		    " | "
		    "/home/teruaki/garakuta-okiba/wnix/grep.scm"
		    " -n=\"" (backslash->slash grep-n) "\""
		    " -k=\"" (backslash->slash grep-k) "\""
		    " -o=\"" (backslash->slash grep-o) "\""))
    ((#/^(\S*)\/cat -n=(\S+) (\S+) \| sort -n=(\S+) -k=(\S+)$/ path)
     (#f dir n target sort-n sort-k)
     (string-append "/home/teruaki/garakuta-okiba/wnix/cat.scm"
		    " -n=\"" (backslash->slash n) "\""
		    " ./root" dir "/" (backslash->slash target)
		    " | "
		    "/home/teruaki/garakuta-okiba/wnix/sort.scm"
		    " -n=\"" (backslash->slash sort-n) "\""
		    " -k=\"" (backslash->slash sort-k) "\""))
    ((#/^(\S*)\/cat -n=(\S+) (\S+) \| sort -n=(\S+) -k=(\S+) \| xsl -f=(\S+)$/ path)
     (#f dir n target sort-n sort-k xsl-f)
     (string-append "/home/teruaki/garakuta-okiba/wnix/cat.scm"
		    " -n=\"" (backslash->slash n) "\""
		    " ./root" dir "/" (backslash->slash target)
		    " | "
		    "/home/teruaki/garakuta-okiba/wnix/sort.scm"
		    " -n=\"" (backslash->slash sort-n) "\""
		    " -k=\"" (backslash->slash sort-k) "\""
		    " | "
		    "/home/teruaki/garakuta-okiba/wnix/xsl.scm"
		    " -f=\"" (backslash->slash xsl-f) "\""))
    ((#/^(\S*)\/cat -n=(\S+) (\S+) \| sort -n=(\S+) -k=(\S+) \| grep -n=(\S+) -k=(\S+) -o=(\S+)$/ path)
     (#f dir n target sort-n sort-k grep-n grep-k grep-o)
     (string-append "/home/teruaki/garakuta-okiba/wnix/cat.scm"
		    " -n=\"" (backslash->slash n) "\""
		    " ./root" dir "/" (backslash->slash target)
		    " | "
		    "/home/teruaki/garakuta-okiba/wnix/sort.scm"
		    " -n=\"" (backslash->slash sort-n) "\""
		    " -k=\"" (backslash->slash sort-k) "\""
		    " | "
		    "/home/teruaki/garakuta-okiba/wnix/grep.scm"
		    " -n=\"" (backslash->slash grep-n) "\""
		    " -k=\"" (backslash->slash grep-k) "\""
		    " -o=\"" (backslash->slash grep-o) "\""))
    ((#/^(\S*)\/cat -n=(\S+) (\S+) \| xsl -f=(\S+)$/ path)
     (#f dir n target xsl-f)
     (string-append "/home/teruaki/garakuta-okiba/wnix/cat.scm"
		    " -n=\"" (backslash->slash n) "\""
		    " ./root" dir "/" (backslash->slash target)
		    " | "
		    "/home/teruaki/garakuta-okiba/wnix/xsl.scm"
		    " -f=\"" (backslash->slash xsl-f) "\""))
    (else #f)))

(define (xml-writer filepath sxml xslt)
  (call-with-output-file filepath (lambda (out)
				    (unless ((if-sxpath "processing-instruction('xml')") sxml)
				      (format out "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"))
				    ;;(when xslt (format out "<?xml-stylesheet type=\"text/xsl\" href=\"~a\"?>\n" xslt))
				    (srl:sxml->xml sxml out))))
